using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.BO.Editoral.Category;
using Portal.Core.DAL;

namespace Portal.GUI.EditoralOffice.MainOffce.NewsEditModules.OtherNews
{
    public partial class OtherNewsEdit : Portal.BO.Editoral.NewsEditModules.NewsModule
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                CategoryHelper.BindListCat(lstCat);
                lstCat.Items.RemoveAt(0);

                //int template = CategoryHelper.getCatInfoAsCategoryRow(Convert.ToInt32(lstCat.SelectedValue)).Cat_ViewNum;

                bindData();
            }
        }

        private void bindData()
        {
            LoadRuntimeSettings();

            string categoryId = Convert.ToString(ReadRuntimeConfig(true, "CategoryID"));
            lstCat.SelectedValue = (categoryId != "" && categoryId != "0") ? categoryId : "0";
			if (lstCat.SelectedIndex == -1) lstCat.SelectedIndex = 0;

            txtNewsNumber.Text = Convert.ToString(ReadRuntimeConfig(true, "NumberOfOtherNews")).ToString();
            if (txtNewsNumber.Text == "")
                txtNewsNumber.Text = "5";
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            save();
			((Portal.GUI.EditoralOffice.MainOffce.editnews.editModule)this.Page).loadPresentationModule(ModuleType, ModuleRef);
        }

        public override void SaveAllProperties()
        {
            save();
        }

        private string getNewsID()
        {
			return Session["newsid"] != null ? Convert.ToString(Session["newsid"]) : "";
        }

        private void save()
        {
            PortalDefinition.Module module = new PortalDefinition.Module();
            module.reference = this.ModuleRef;
            module.type = this.ModuleType;
            module.LoadRuntimeProperties();

            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "NewsID", getNewsID());
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "CategoryID", lstCat.SelectedValue);
            module.moduleRuntimeSettings.SetRuntimePropertyValue(true, "NumberOfOtherNews", txtNewsNumber.Text);

            module.SaveRuntimeSettings();
        }

        private Hashtable getNews(string newsIds)
        {
            Hashtable table = new Hashtable();
            using (MainDB db = new MainDB())
            {
                string sql = "Select News_ID, News_Title from News where News_ID in (" + newsIds + ")";
                DataTable dt = db.SelectQuery(sql);
                foreach (DataRow row in dt.Rows)
                {
                    table.Add(Convert.ToString(row["News_ID"]), Convert.ToString(row["News_Title"]));
                }
                db.Close();
            }
            return table;
        }

        protected void btnOK_Click1(object sender, EventArgs e)
        {
            save();

			((Portal.GUI.EditoralOffice.MainOffce.editnews.editModule)this.Page).loadPresentationModule(ModuleType, ModuleRef);

			Literal js = Page.FindControl("js") as Literal;
			if (js != null)
			{
				js.Text = "<script>saveModule();</script>";
			}
        }
    }
}